﻿using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;

namespace Coater.Helper
{
    public class XMLHelper
    {

        //得到配置项对象实例信息
        public static T GetSettings<T>(string fileName)
        {

            XmlSerializer serializer = new XmlSerializer(typeof(T));
            try
            {
                if (!Directory.Exists("XMLconfig"))
                {
                    Directory.CreateDirectory("XMLconfig");
                }
                string fName = "XMLconfig/" + fileName + ".xml";
                if (!File.Exists(fName))
                {
                    FileStream fsNew = new FileStream(fName, FileMode.Create);
                    var classInstance = CreateClassInstanceByName(typeof(T), new object[] { });
                    serializer.Serialize(fsNew, classInstance);
                    fsNew.Close();
                }
                FileStream fs = new FileStream(fName, FileMode.Open);
                T data = (T)serializer.Deserialize(fs)!;//反序列化
                fs.Close();
                return data;
            }
            catch { return CreateClassInstanceByName(typeof(T), new object[] { }); }
        }

        public static T GetSettings_FullName<T>(string fileName)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));
            try
            {
                if (!File.Exists(fileName))
                {
                    FileStream fsNew = new FileStream(fileName, FileMode.Create);
                    var classInstance = CreateClassInstanceByName(typeof(T), new object[] { });
                    serializer.Serialize(fsNew, classInstance);
                    fsNew.Close();
                }
                FileStream fs = new FileStream(fileName, FileMode.Open);
                T data = (T)serializer.Deserialize(fs)!;//反序列化
                fs.Close();
                return data;
            }
            catch { return CreateClassInstanceByName(typeof(T), new object[] { }); }
        }

        //保存配置数据
        public static void SaveSettings<T>(string fileName, T data)
        {
            if (!Directory.Exists("XMLconfig"))
            {
                Directory.CreateDirectory("XMLconfig");
            }
            string fName = "XMLconfig/" + fileName + ".xml";
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            //序列化对象
            FileStream fs = new FileStream(fName, FileMode.Create);
            serializer.Serialize(fs, data);
            fs.Close();
        }

        public static void SaveSettings_FullName<T>(string fileName, T data)
        {
            XmlSerializer serializer = new XmlSerializer(typeof(T));

            //序列化对象
            FileStream fs = new FileStream(fileName, FileMode.Create);
            serializer.Serialize(fs, data);
            fs.Close();
        }


        private static dynamic CreateClassInstanceByName(Type type, object[] parameters)
        {
            if (type == null)
            {
                throw new TypeAccessException("Type conversion failed.");
            }

            var objInstance = Activator.CreateInstance(type, parameters);
            return objInstance;
        }

    }
}
